package com.tushen.datacenter.ts;

import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.common.util.Util;
import com.common.view.ViewAdjust;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class ItemAdapter extends BaseAdapter {

	private static final String TAG = "ItemAdapter";
	private LayoutInflater mInflater;
	private ArrayList<String> mList = null;
    private ArrayList<Integer> mColorList = null;

	public ItemAdapter(LayoutInflater inflater) {
		mInflater = inflater;
	}

	@Override
	public int getCount() {

		if (mList == null) {
			Log.d(TAG, "getCount mList == null ...size = 0");
			return 0;
		}
		//Log.d(TAG, "getCount size = " + mList.size());
		return mList.size();
	}

	public void clear() {

		mList = null;
        mColorList = null;
	}

	public void setList(ArrayList<String> list,ArrayList<Integer> colorList) {

		clear();
		if (list == null || list.size() <= 0) {
			notifyDataSetChanged();
			return;
		}
		mList = list;//Util.safeClone(list);
        mColorList = colorList;
		Log.d(TAG, "setList size = " + mList.size());
		notifyDataSetChanged();
	}

	public ArrayList<String> getList() {
		return mList;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		

		if(!Util.checkBoundIsOk(mList, position)){
			return null;
		}

		HolderView holder ;

		if(convertView  == null){
			convertView = mInflater.inflate(R.layout.adapter_item, parent,false);
			ViewAdjust.adjustLayoutParams(mInflater.getContext(), convertView.findViewById(R.id.inner_root_view));
			holder = new HolderView();
			holder.imageView = (CircleImageView)convertView.findViewById(R.id.item_image);
			holder.textView1 = (TextView)convertView.findViewById(R.id.item_text1);
            holder.textView2 = (TextView)convertView.findViewById(R.id.item_text2);
			convertView.setTag(holder);
		}else{
			holder = (HolderView)convertView.getTag();
		}
        holder.imageView.setImageDrawable(new ColorDrawable(mColorList.get(position)));
		//Log.d(TAG,"getView pos =  "+position+" "+mList.get(position));

        holder.textView1.setText(mList.get(position).split(" ")[0]);
        holder.textView2.setText(mList.get(position).split(" ")[1]);

		return convertView;
	}

	
	private static class HolderView {
		 CircleImageView imageView;
		 TextView textView1;
         TextView textView2;
	}

}
